package org.jeecg.modules.online.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jeecg.modules.online.entity.DiagramConfiguration;
import org.jeecg.modules.online.mapper.DiagramConfigurationMapper;
import org.jeecg.modules.online.service.IDiagramConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 图表配置
 */
@Service
public class DiagramConfigurationServiceImpl extends ServiceImpl<DiagramConfigurationMapper, DiagramConfiguration>
		implements IDiagramConfigurationService {

	@Autowired
	private DiagramFieldConfigurationServiceImpl diagramFieldConfigurationService;
	@Resource
	private DiagramConfigurationMapper diagramConfigurationMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(DiagramConfiguration diagramConfiguration) {
		// 保存主表信息
		this.save(diagramConfiguration);
		// 保存子表信息
		if (diagramConfiguration != null && diagramConfiguration.getDiagramFieldConfigurationList() != null
				&& !diagramConfiguration.getDiagramFieldConfigurationList().isEmpty()) {
			diagramConfiguration.getDiagramFieldConfigurationList()
					.forEach(dfc -> dfc.setDiagramCode(diagramConfiguration.getCode()));
			this.diagramFieldConfigurationService.saveBatch(diagramConfiguration.getDiagramFieldConfigurationList());
		}

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void edit(DiagramConfiguration diagramConfiguration) {
		this.updateById(diagramConfiguration);
		// 更新子表信息
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("DIAGRAM_CODE", diagramConfiguration.getCode());
		this.diagramFieldConfigurationService.removeByMap(columnMap);
		this.diagramFieldConfigurationService.saveBatch(diagramConfiguration.getDiagramFieldConfigurationList());

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteById(String id) {
		DiagramConfiguration diagramConfiguration = super.getById(id);
		this.removeById(id);
		// 删除子表信息
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("DIAGRAM_CODE", diagramConfiguration.getCode());
		this.diagramFieldConfigurationService.removeByMap(columnMap);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String ids) {
		List<String> idList = Arrays.asList(ids.split(","));
		QueryWrapper<DiagramConfiguration> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("ID", idList);
		List<DiagramConfiguration> diagramConfigurationList = super.list(queryWrapper);
		this.removeByIds(idList);
		// 删除子表信息
		if (diagramConfigurationList != null && !diagramConfigurationList.isEmpty()) {
			Map<String, Object> columnMap = new HashMap<>();
			for (DiagramConfiguration diagramConfiguration : diagramConfigurationList) {
				columnMap.clear();
				columnMap.put("DIAGRAM_CODE", diagramConfiguration.getCode());
				this.diagramFieldConfigurationService.removeByMap(columnMap);
			}
		}
	}

	@Override
	public List selectBySql(String sql) {
		return this.diagramConfigurationMapper.selectBySql(sql);
	}

}
